<template>
  <span v-if="readonly">
    <slot name="formName" :emp="{ FormName: formName, formId: value }">
      {{ formName }}
    </slot>
  </span>
  <div v-else>
    <el-input
      v-model="formName"
      :readonly="true"
      :placeholder="placeholder"
      :clearable="true"
    >
      <template slot="suffix">
        <icon class="icon red" icon="el-icon-delete" @click="clear" />
        <icon class="icon" icon="el-icon-edit" @click="openForm" />
        <icon v-if="value != null && type != 2" class="icon" icon="el-icon-view" @click="openView" />
      </template>
    </el-input>
    <formChiose :title="title" :company-id="companyId" :is-allow-null="isAllowNull" :prt-form-classify="prtFormClassify" :form-classify="formClassify" :form-type="formType" :visible.sync="visible" :form-id.sync="formId" @save="save" />
    <formPreviewModel :form-id="value" :visible.sync="preVisible" />
  </div>
</template>

<script>
import formChiose from './formChiose.vue'
import { Get } from '@/customForm/api/form'
import store from '@/store'
import formPreviewModel from '../preview/formPreviewModel.vue'
export default {
  components: {
    formChiose,
    formPreviewModel
  },
  props: {
    title: {
      type: String,
      default: '选择表单'
    },
    formClassify: {
      type: String,
      default: null
    },
    formType: {
      type: Array,
      default: null
    },
    placeholder: {
      type: String,
      default: null
    },
    companyId: {
      type: String,
      default: () => {
        return store.getters.curComId
      }
    },
    prtFormClassify: {
      type: String,
      default: null
    },
    isAllowNull: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    value: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      form: null,
      formName: null,
      formId: null,
      type: null,
      preVisible: false,
      visible: false
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val && val !== '') {
          this.reset()
        } else {
          this.formId = null
          this.form = null
          this.type = null
          this.formName = null
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    async reset() {
      this.form = await Get(this.value)
      this.formName = this.form.FormName
      this.type = this.form.FormType
      this.formId = this.value
    },
    clear() {
      this.formName = null
      this.form = null
      this.type = null
      this.$emit('input', null)
      this.$emit('change', null, null)
    },
    openView() {
      this.preVisible = true
    },
    closeChoice() {
      this.empVisible = false
    },
    openForm() {
      this.visible = true
    },
    save(e) {
      this.visible = false
      this.form = e.form
      if (this.form == null) {
        this.formName = null
        this.$emit('input', null)
        this.$emit('change', null, null)
      } else {
        this.formName = this.form.FormName
        this.type = this.form.FormType
        this.$emit('input', e.formId)
        this.$emit('change', e.formId, this.form)
      }
    }
  }
}
</script>
<style scoped>
.icon {
  font-size: 20px;
  margin-right: 5px;
  color: #1890ff;
  cursor: pointer;
}
.red {
  color: #e4393c !important;
}
</style>
